package com.example.scene;

import org.andengine.engine.camera.Camera;
import org.andengine.engine.handler.timer.ITimerCallback;
import org.andengine.engine.handler.timer.TimerHandler;
import org.andengine.entity.scene.menu.MenuScene;
import org.andengine.entity.scene.menu.MenuScene.IOnMenuItemClickListener;
import org.andengine.entity.scene.menu.item.IMenuItem;
import org.andengine.entity.scene.menu.item.SpriteMenuItem;
import org.andengine.entity.scene.menu.item.decorator.ScaleMenuItemDecorator;
import org.andengine.entity.sprite.Sprite;
import org.andengine.opengl.util.GLState;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.base.BaseScene;
import com.example.fruit.GameActivity;
import com.example.fruit.R;
import com.example.fruit.R.string;
import com.example.manager.PauseableTimerHandler;
import com.example.manager.ResourcesManager;
import com.example.manager.SceneManager.SceneType;

public class LoginScene extends BaseScene{
	private static String SOAP_ACTION = "http://tempuri.org/login";
	private static String METHOD_login = "login";
	private static String NAMESPACE = "http://tempuri.org/";
	private static String URL = "http://webservicedemo.somee.com/servicehighscore.asmx?wsdl";
	PauseableTimerHandler p;
	boolean flag=false;
	static boolean check;
	public String user,pass;
	public LoginScene(final String user,final String pass)
	{
		this.user=user;
		this.pass=pass;
		MyAsytask task=new MyAsytask();
		task.execute();
		
		p=new PauseableTimerHandler(0.5f, true, new ITimerCallback() {
			
			@Override
			public void onTimePassed(TimerHandler pTimerHandler) {
				// TODO Auto-generated method stub
				if(flag){
					p.mPause=true;
					p.setAutoReset(false);
					ResourcesManager.getInstance().engine.unregisterUpdateHandler(p);
					login = activity.getSharedPreferences("login", activity.MODE_PRIVATE);
					loginEditor = login.edit();
					if(check)
					{
						activity.runOnUiThread(new Runnable() {
							
							@Override
							public void run() {
								/*login = activity.getSharedPreferences("login", activity.MODE_PRIVATE);
								loginEditor = login.edit();*/
								loginEditor.putBoolean("onlogin", true);
								loginEditor.putString("user", user );
								loginEditor.putString("pass", pass );
								loginEditor.commit();
								activity.message=null;
								activity.message="Login thanh cong!";
								activity.showDialog(4);
							}
						});
					}
					else{
						activity.runOnUiThread(new Runnable() {
							
							@Override
							public void run() {
								// TODO Auto-generated method stub
								
								loginEditor.putBoolean("onlogin", false);
								loginEditor.putString("user", "unknown");
								loginEditor.putString("pass", "nopass");
								loginEditor.commit();
								activity.message=null;
								activity.message="Login that bai!";
								activity.showDialog(4);
							}
						});
					}
				}
			}
		});
		ResourcesManager.getInstance().engine.registerUpdateHandler(p);
	}
	int k=0;
	SharedPreferences login ;
	SharedPreferences.Editor loginEditor;
	class MyAsytask extends AsyncTask<Void, Void, String>
	{
		
		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
			activity.runOnUiThread(new Runnable() {
				
				@Override
				public void run() {
					// TODO Auto-generated method stub
					 	activity.showDialog(8);
				}
			});
		}

		@Override
		protected String doInBackground(Void... params) {
			// TODO Auto-generated method stub
			SoapObject request = new SoapObject(NAMESPACE, METHOD_login);
			
			// Use this to add parameters
			request.addProperty("username", user);
			request.addProperty("pass", pass);
			// Declare the version of the SOAP request
			SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
					SoapEnvelope.VER11);
			String s ="";
			envelope.setOutputSoapObject(request);
			envelope.dotNet = true;
			try {
				HttpTransportSE androidHttpTransport = new HttpTransportSE(
						URL);

				// this is the actual part that will call the webservice
				androidHttpTransport.call(SOAP_ACTION, envelope);
				
				// Get the SoapResult from the envelope body.
				SoapObject result = (SoapObject) envelope.bodyIn; //the loadscoreresponse
				if(result!=null){
					s=result.getProperty(0).toString();	
				}
				Log.d("user",user+":"+pass);
				
				//s=(Boolean)result.getProperty(0);
				//Log.d("result",pii.getProperty(0).toString()+"");
			} catch (Exception e) {
				e.printStackTrace();
				Log.d("asdad",e.getMessage());
			}
			return s;
		}
		@Override
		protected void onPostExecute(final String result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			activity.runOnUiThread(new Runnable() {
				
				@Override
				public void run() {
					// TODO Auto-generated method stub
					 	activity.dismissDialog(8);
					 	Log.d("ạksdnkasd",result+"");
				}
			});
			if(result.equals("true"))
			{
				check=true;
			}else
			{
				check=false;
			}
			Log.d("ạksdnkasd",result+"");
			this.cancel(true);	
			flag=true;
		}
	}

	@Override
	public void createScene() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onBackKeyPressed() {
		// TODO Auto-generated method stub
		System.exit(0);
	}

	@Override
	public SceneType getSceneType() {
		// TODO Auto-generated method stub
		return SceneType.SCENE_LOGIN;
	}

	@Override
	public void disposeScene() {
		// TODO Auto-generated method stub
		
	}
}
